﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// DatePair扩展
    /// </summary>
    public static class DatePairExtension
    {
        /// <summary>
        /// 时间对转sql语句
        /// </summary>
        /// <param name="dp"></param>
        /// <param name="begField">开始时间Sql字段</param>
        /// <param name="endField">结束时间Sql字段</param>
        /// <param name="format"></param>        
        public static string GToWhere(this GDatePair dp,string begField,string endField,string format="")
        {
            if (dp.GIsEmpty(true)) return string.Empty;

            if (begField.GIsEmpty(true) || endField.GIsEmpty(true)) throw new Exception("对应字段不能为空");

            if (format.GIsEmpty(true)) format = "yyyy-MM-dd HH:mm:ss";

            if (dp.Begin != null && dp.Final != null)
                return $"{begField}>='{((DateTime)dp.Begin).ToString(format)}' And {endField}<='{((DateTime)dp.Final).ToString(format)}'";
            else if (dp.Begin != null)
                return $"{begField}>='{((DateTime)dp.Begin).ToString(format)}'";
            else if (dp.Final!=null)
                return $"{begField}>='{((DateTime)dp.Final).ToString(format)}'";
            return string.Empty;
        }
    }
}
